﻿namespace Yipusoft.BaseDoc.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using Yipusoft.BaseDoc.DBUtility;
    using Yipusoft.BaseDoc.Model;

    public class Progress
    {
        public bool AddProgress(ProgressInfo progressinfo)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("DepartmentID", SqlDbType.VarChar, 50), new SqlParameter("FullName", SqlDbType.NVarChar, 50), new SqlParameter("ParentID", SqlDbType.VarChar, 50), new SqlParameter("ItemLevel", SqlDbType.Int, 4), new SqlParameter("CreateUser", SqlDbType.VarChar, 50), new SqlParameter("CreateDate", SqlDbType.DateTime), new SqlParameter("IsPass", SqlDbType.Bit), new SqlParameter("OrderID", SqlDbType.Int, 4), new SqlParameter("CreateYear", SqlDbType.Int, 4) };
                commandParameters[0].Value = progressinfo.DepartmentID;
                commandParameters[1].Value = progressinfo.FullName;
                commandParameters[2].Value = progressinfo.ParentID;
                commandParameters[3].Value = progressinfo.ItemLevel;
                commandParameters[4].Value = progressinfo.CreateUser;
                commandParameters[5].Value = progressinfo.CreateDate;
                commandParameters[6].Value = progressinfo.IsPass;
                commandParameters[7].Value = progressinfo.OrderID;
                commandParameters[8].Value = progressinfo.CreateYear;
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "JobProgress_Add", commandParameters) > 0);
            }
        }

        public bool DeleteProgress(string departmentID)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("DepartmentID", SqlDbType.VarChar, 50) };
                commandParameters[0].Value = departmentID;
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "JobProgress_Delete", commandParameters) > 0);
            }
        }

        public List<ProgressInfo> GetList(string where)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@where", SqlDbType.NVarChar, 500) };
                commandParameters[0].Value = where;
                List<ProgressInfo> list = new List<ProgressInfo>();
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "JobProgress_GetListByWhere", commandParameters);
                while (reader.Read())
                {
                    ProgressInfo item = new ProgressInfo();
                    item.DepartmentID = this.GetValue(reader["DepartmentID"]) ? reader["DepartmentID"].ToString() : "";
                    item.FullName = this.GetValue(reader["FullName"]) ? reader["FullName"].ToString() : "";
                    item.ParentID = this.GetValue(reader["ParentID"]) ? reader["ParentID"].ToString() : "";
                    item.CreateUser = this.GetValue(reader["CreateUser"]) ? reader["CreateUser"].ToString() : "";
                    item.CreateDate = this.GetValue(reader["CreateDate"]) ? ((DateTime) reader["CreateDate"]) : DateTime.MinValue;
                    item.ItemLevel = this.GetValue(reader["ItemLevel"]) ? ((int) reader["ItemLevel"]) : -1;
                    item.IsPass = this.GetValue(reader["IsPass"]) ? ((bool) reader["IsPass"]) : false;
                    item.OrderID = this.GetValue(reader["OrderID"]) ? ((int) reader["OrderID"]) : -1;
                    item.CreateYear = this.GetValue(reader["CreateYear"]) ? ((int) reader["CreateYear"]) : -1;
                    list.Add(item);
                }
                return list;
            }
        }

        public List<ProgressInfo> GetList(string where, int pageIndex, int pageSize, out int count)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@Where", SqlDbType.VarChar, 500), new SqlParameter("@PageIndex", SqlDbType.Int, 4), new SqlParameter("@PageSize", SqlDbType.Int, 4), new SqlParameter("@Count", SqlDbType.Int, 4) };
                commandParameters[0].Value = where;
                commandParameters[1].Value = pageIndex;
                commandParameters[2].Value = pageSize;
                commandParameters[3].Direction = ParameterDirection.Output;
                List<ProgressInfo> list = new List<ProgressInfo>();
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "JobProgress_GetListByWhere2", commandParameters);
                while (reader.Read())
                {
                    ProgressInfo item = new ProgressInfo();
                    item.DepartmentID = this.GetValue(reader["DepartmentID"]) ? reader["DepartmentID"].ToString() : "";
                    item.FullName = this.GetValue(reader["FullName"]) ? reader["FullName"].ToString() : "";
                    item.ParentID = this.GetValue(reader["ParentID"]) ? reader["ParentID"].ToString() : "";
                    item.CreateUser = this.GetValue(reader["CreateUser"]) ? reader["CreateUser"].ToString() : "";
                    item.CreateDate = this.GetValue(reader["CreateDate"]) ? ((DateTime) reader["CreateDate"]) : DateTime.MinValue;
                    item.ItemLevel = this.GetValue(reader["ItemLevel"]) ? ((int) reader["ItemLevel"]) : -1;
                    item.IsPass = this.GetValue(reader["IsPass"]) ? ((bool) reader["IsPass"]) : false;
                    item.OrderID = this.GetValue(reader["OrderID"]) ? ((int) reader["OrderID"]) : -1;
                    item.CreateYear = this.GetValue(reader["CreateYear"]) ? ((int) reader["CreateYear"]) : -1;
                    list.Add(item);
                }
                count = (int) commandParameters[3].Value;
                return list;
            }
        }

        public int GetMaxID(string DepartmentID)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                string commandText = "Select Max(Cast(DepartmentID as int)) From JobProgress Where Left(DepartmentID,4)='" + DepartmentID.Substring(0, 4) + "'";
                object obj2 = SqlHelper.ExecuteScalar(connection, CommandType.Text, commandText, null);
                return (((obj2 == null) || (obj2 == DBNull.Value)) ? -1 : ((int) obj2));
            }
        }

        public ProgressInfo GetProgress(string DepartmentID)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DepartmentID", SqlDbType.VarChar, 500) };
                commandParameters[0].Value = DepartmentID;
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.Text, "Select * From [JobProgress] Where DepartmentID=@DepartmentID", commandParameters);
                if (reader.Read())
                {
                    ProgressInfo info = new ProgressInfo();
                    info.DepartmentID = this.GetValue(reader["DepartmentID"]) ? reader["DepartmentID"].ToString() : "";
                    info.FullName = this.GetValue(reader["FullName"]) ? reader["FullName"].ToString() : "";
                    info.ParentID = this.GetValue(reader["ParentID"]) ? reader["ParentID"].ToString() : "";
                    info.CreateUser = this.GetValue(reader["CreateUser"]) ? reader["CreateUser"].ToString() : "";
                    info.CreateDate = this.GetValue(reader["CreateDate"]) ? ((DateTime) reader["CreateDate"]) : DateTime.MinValue;
                    info.ItemLevel = this.GetValue(reader["ItemLevel"]) ? ((int) reader["ItemLevel"]) : -1;
                    info.IsPass = this.GetValue(reader["IsPass"]) ? ((bool) reader["IsPass"]) : false;
                    info.OrderID = this.GetValue(reader["OrderID"]) ? ((int) reader["OrderID"]) : -1;
                    info.CreateYear = this.GetValue(reader["CreateYear"]) ? ((int) reader["CreateYear"]) : -1;
                    return info;
                }
                return null;
            }
        }

        private bool GetValue(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            if (obj == DBNull.Value)
            {
                return false;
            }
            return true;
        }

        public bool UpdateProgress(ProgressInfo progressinfo)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("DepartmentID", SqlDbType.VarChar, 50), new SqlParameter("FullName", SqlDbType.NVarChar, 50), new SqlParameter("ParentID", SqlDbType.VarChar, 50), new SqlParameter("ItemLevel", SqlDbType.Int, 4), new SqlParameter("CreateUser", SqlDbType.VarChar, 50), new SqlParameter("CreateDate", SqlDbType.DateTime), new SqlParameter("IsPass", SqlDbType.Bit), new SqlParameter("OrderID", SqlDbType.Int, 4), new SqlParameter("CreateYear", SqlDbType.Int, 4) };
                commandParameters[0].Value = progressinfo.DepartmentID;
                commandParameters[1].Value = progressinfo.FullName;
                commandParameters[2].Value = progressinfo.ParentID;
                commandParameters[3].Value = progressinfo.ItemLevel;
                commandParameters[4].Value = progressinfo.CreateUser;
                commandParameters[5].Value = progressinfo.CreateDate;
                commandParameters[6].Value = progressinfo.IsPass;
                commandParameters[7].Value = progressinfo.OrderID;
                commandParameters[8].Value = progressinfo.CreateYear;
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "JobProgress_Update", commandParameters) > 0);
            }
        }
    }
}

